package top.tungsword.webconsole.biz.service.app;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.tungsword.webconsole.biz.param.AppParam;
import top.tungsword.webconsole.biz.response.WebReturnInfo;
import top.tungsword.webconsole.manager.i18n.I18nService;
import top.tungsword.webconsole.biz.service.base.BaseBiz;
import top.tungsword.webconsole.dal.entity.SsoApp;
import top.tungsword.webconsole.dal.mapper.SsoAppMapper;
import top.tungsword.webconsole.util.ParamCheckUtil;

import java.util.List;

import static top.tungsword.webconsole.biz.constant.I18nConstants.*;

@Service
public class AppBiz extends BaseBiz {

    @Autowired
    private SsoAppMapper ssoAppMapper;

    @Autowired
    private I18nService i18nService;

    /**
     * 功能描述: <br>
     *     查询应用
     * @Param param
     * @Return java.util.List<top.tungsword.webconsole.dal.entity.SsoApp>
     * @Author chenji.yu
     * @Date 2020/4/30 10:38
     */
    public List<SsoApp> findAppList(AppParam param){
        SsoApp ssoApp = new SsoApp();
        BeanUtils.copyProperties(param, ssoApp);
        return ssoAppMapper.selectBySsoApp(ssoApp);
    }

    /**
     * 功能描述: <br>
     *     新增应用
     * @Param param
     * @Return top.tungsword.webconsole.biz.response.WebReturnInfo<java.lang.String>
     * @Author chenji.yu
     * @Date 2020/4/30 10:38
     */
    public WebReturnInfo<String> addApp(SsoApp param){
        //参数检查
        String message = checkParam(param);
        if(StringUtils.isNotEmpty(message)){
            WebReturnInfo<String> webReturnInfo = WebReturnInfo.FAIL;
            webReturnInfo.setMessage(message);
            return webReturnInfo;
        }

        //新增应用到数据库
        try {
            ssoAppMapper.insertSelective(param);
        }catch (Exception e){
            //新增失败返回
            WebReturnInfo<String> webReturnInfo = WebReturnInfo.FAIL;
            webReturnInfo.setMessage(i18nService.getMessage(SSO_APP_ADD_ERROR));
            logger.error("【"+param.getAppId()+":"+param.getAppName()+"】"+i18nService.getMessage(SSO_APP_ADD_ERROR));
            logger.error("Add App Failed Detail：",e);
            return webReturnInfo;
        }

        //新增成功返回
        WebReturnInfo<String> webReturnInfo = WebReturnInfo.SUCCESS;
        webReturnInfo.setMessage(i18nService.getMessage(SSO_APP_ADD_SUCCESS));
        logger.info("【"+param.getAppId()+":"+param.getAppName()+"】"+i18nService.getMessage(SSO_APP_ADD_SUCCESS));
        return webReturnInfo;
    }

    /**
     * 功能描述: <br>
     *     删除系统
     * @Param param
     * @Return top.tungsword.webconsole.biz.response.WebReturnInfo<java.lang.String>
     * @Author chenji.yu
     * @Date 2020/4/30 17:09
     */
    public WebReturnInfo<String> deleteApp(SsoApp param){
        //参数判断
        if(param.getId() < 0){
            WebReturnInfo<String> webReturnInfo = WebReturnInfo.FAIL;
            webReturnInfo.setMessage(i18nService.getMessage(SSO_APP_DELETE_ERROR));
        }

        //TODO 删除系统
        //是否有角色，有不能删除
        //没有使用的角色，删除系统SsoApp表，同时删除对应的菜单表
        //删除系统
        try {
            ssoAppMapper.deleteByPrimaryKey(param.getId());
        }catch (Exception e){
            logger.error("【"+param.getAppId()+":"+param.getAppName()+"】"+i18nService.getMessage(SSO_APP_DELETE_ERROR));
            logger.error("Delete App Failed Detail：",e);
            WebReturnInfo<String> webReturnInfo = WebReturnInfo.FAIL;
            webReturnInfo.setMessage("【"+param.getAppId()+":"+param.getAppName()+"】" + i18nService.getMessage(SSO_APP_DELETE_ERROR));
            return webReturnInfo;
        }

        //删除成功返回
        WebReturnInfo<String> webReturnInfo = WebReturnInfo.SUCCESS;
        webReturnInfo.setMessage("【"+param.getAppId()+":"+param.getAppName()+"】"+i18nService.getMessage(SSO_APP_DELETE_SUCCESS));
        return webReturnInfo;

    }

    private String checkParam(SsoApp param){
        //参数检查
        StringBuffer message = new StringBuffer();
        ParamCheckUtil.notNullCheck(param.getAppId(), i18nService.getMessage(SSO_APPID_NULL_ERROR), message);
        ParamCheckUtil.notNullCheck(param.getAppName(), i18nService.getMessage(SSO_APPNAME_NULL_ERROR), message);
        ParamCheckUtil.lengthCheck(param.getAppId(), 16, i18nService.getMessage(SSO_APPID_MAXLENGTH_ERROR), message);
        ParamCheckUtil.lengthCheck(param.getAppName(), 32, i18nService.getMessage(SSO_APPNAME_MAXLENGTH_ERROR), message);
        return message.toString();
    }

    /**
     * 更新系统
     * @param param
     * @return
     */
    public WebReturnInfo<String> updateApp(SsoApp param) {
        //参数检查
        String message = checkParam(param);
        if(StringUtils.isNotEmpty(message)){
            WebReturnInfo<String> webReturnInfo = WebReturnInfo.FAIL;
            webReturnInfo.setMessage(message);
            return webReturnInfo;
        }

        try{
            ssoAppMapper.updateByPrimaryKey(param);
        }catch (Exception e){
            //更新失败返回
            WebReturnInfo<String> webReturnInfo = WebReturnInfo.FAIL;
            webReturnInfo.setMessage(i18nService.getMessage(SSO_APP_UPDATE_ERROR));
            logger.error("【"+param.getAppId()+":"+param.getAppName()+"】"+i18nService.getMessage(SSO_APP_UPDATE_ERROR));
            logger.error("Update App Failed Detail：",e);
            return webReturnInfo;
        }

        //更新成功返回
        WebReturnInfo<String> webReturnInfo = WebReturnInfo.SUCCESS;
        webReturnInfo.setMessage(i18nService.getMessage(SSO_APP_UPDATE_SUCCESS));
        logger.info("【"+param.getAppId()+":"+param.getAppName()+"】"+i18nService.getMessage(SSO_APP_UPDATE_SUCCESS));
        return webReturnInfo;
    }
}
